#include "tinyxml/tinyxml.h"
#include "XMLBuilder.h"
#include <map>

char* createTractArrayXML(map<double,double> tractMap)
{
	const char* TemlpXML = "<?xml version=\"1.0\" standalone=\"no\">\n"
		"<out>\n</out>";

	TiXmlDocument DocXMLOut;
	DocXMLOut.Parse(TemlpXML);
	TiXmlElement* rootElement = DocXMLOut.RootElement();
	char x[50];

	map<double,double>::iterator it;


	for(it = tractMap.begin();it !=tractMap.end();++it){

		TiXmlElement* entry = new TiXmlElement( "entry" );
		TiXmlElement* inputVoltage = new TiXmlElement( "inputVoltage");
		TiXmlElement* outputCurrency = new TiXmlElement( "outputCurrency");

		TiXmlText * text;

		//use %f or %g 
		sprintf(x,"%f",it->first);
		text = new TiXmlText(x);
		inputVoltage->InsertEndChild(*text);

		//use %f or %g 
		sprintf(x,"%f",it->second);
		text = new TiXmlText(x);

		outputCurrency->InsertEndChild(*text);


		entry->InsertEndChild(*inputVoltage);
		entry->InsertEndChild(*outputCurrency);

		rootElement->InsertEndChild(*entry);
	}	

	TiXmlPrinter printer;
	DocXMLOut.Accept( &printer );

	const char* xml = printer.CStr();
	char* res=new char[strlen(xml)];
	strcpy(res, xml);
	return res;
}



char* createParallelTwoTractsArrayXML(map<double,double> tractMap){


	const char* TemlpXML = "<?xml version=\"1.0\" standalone=\"no\">\n"
		"<out>\n</out>";

	TiXmlDocument DocXMLOut;
	DocXMLOut.Parse(TemlpXML);
	TiXmlElement* rootElement = DocXMLOut.RootElement();
	char x[50];

	map<double,double>::iterator it;


	for(it = tractMap.begin();it !=tractMap.end();++it){

		TiXmlElement* entry = new TiXmlElement( "entry" );
		TiXmlElement* inputCurrency = new TiXmlElement( "inputCurrency");
		TiXmlElement* outputVoltage = new TiXmlElement( "outputVoltage");

		TiXmlText * text;

		//use %f or %g 
		sprintf(x,"%f",it->first);
		text = new TiXmlText(x);
		inputCurrency->InsertEndChild(*text);

		//use %f or %g 
		sprintf(x,"%f",it->second);
		text = new TiXmlText(x);

		outputVoltage->InsertEndChild(*text);


		entry->InsertEndChild(*inputCurrency);
		entry->InsertEndChild(*outputVoltage);

		rootElement->InsertEndChild(*entry);
	}	

	TiXmlPrinter printer;
	DocXMLOut.Accept( &printer );

	const char* xml = printer.CStr();
	char* res=new char[strlen(xml)];
	strcpy(res, xml);
	return res;
}